package com.xyhuo.leetcode.january;

import com.xyhuo.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 前序遍历
 * https://leetcode.cn/problems/binary-tree-preorder-traversal/
 *
 * @time 2024/1/18
 */
public class PreorderTraversal {

    public List<Integer> preorderTraversal(TreeNode root) {
        search(root);
        return ans;
    }

    List<Integer> ans = new ArrayList<>();

    private void search(TreeNode root) {
        if (null == root) {
            return;
        }
        ans.add(root.val);
        search(root.left);
        search(root.right);
    }
}
